package com.community.service.impl;

import com.community.annotation.SysLog;
import com.community.base.BaseService;
import com.community.service.ISolrService;
import com.community.util.SolrUtil;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.stereotype.Service;

import java.io.IOException;


@Service("iSolrService")
public class SolrServiceImpl extends BaseService implements ISolrService {

    private HttpSolrServer solrServer = SolrUtil.getSolrServer();

    /**
     * 添加至服务器
     * @param document
     * @throws IOException
     * @throws SolrServerException
     */
    @SysLog("增加内容至solr服务器")
    public void add(SolrInputDocument document) throws IOException, SolrServerException {
        //添加大字段至solr服务器
        solrServer.add(document);
        solrServer.commit();
    }


    /**
     * 搜素相关内容
     * @param solrQuery 搜索条件
     * @return
     * @throws Exception
     */
    @SysLog("查询")
    public SolrDocumentList query(SolrQuery solrQuery) throws Exception {
        return solrServer.query(solrQuery).getResults();
    }

    /**
     * 删除索引
     * @param delQuery
     */
    @SysLog("solr删除索引")
    public void del(String delQuery){
        try {
            solrServer.deleteByQuery(delQuery);
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



}
